#include <graphics.h>

ege::mouse_msg GMouseMsg;

class BUTTON {
	unsigned int UID;
	int X, Y, Width, Height;
	const int HOVER, RELEASED, PRESSED;
	ege::PIMAGE Hover, Released, Pressed, Click;
	//          悬停      释放      按下   点击


	
	void CopyImage (ege::PIMAGE Dst, ege::PIMAGE Src) {
		// 复制图像
		ege::getimage (Dst, Src, 0, 0, ege::getwidth (Dst), ege::getheight (Dst));
	}
	int Max (int a, int b) {
		// 最大值
		return a > b ? a : b;
	}
	int Min (int a, int b) {
		// 最小值
		return a > b ? b : a;
	}
	int CheckMouseEvent (int X, int Y) {
		// 根据 click 里面的内容检测是否该响应按钮事件
		if (ege::getpixel (GMouseMsg.x - X, GMouseMsg.y -Y, Click)) {
			if (GMouseMsg.is_left () && !GMouseMsg.is_up ()) {
				return PRESSED;
			} else {
				return HOVER;
			}
		} else {
			return RELEASED;
		}
	}
	static unsigned int GetUID (void) {
		static unsigned int uid = 0;
		uid = uid + 1;
		return uid;
	}


public:
	BUTTON (int X, int Y, int Width, int Height, int UID = GetUID ()):HOVER (1),RELEASED (2), PRESSED (3) {
		// 初始化
		// 得多写几个初始化函数，用以初始化按钮，比如用 ege::PIMAGE 初始化
		// 之类的，
		Hover    = ege::newimage (Width, Height);
		Released = ege::newimage (Width, Height);
		Pressed  = ege::newimage (Width, Height);
		Click    = ege::newimage (Width, Height);

		// 绘制按钮
		ege::setfillcolor (GREEN, Hover);
		ege::setfillcolor (WHITE, Released);
		ege::setfillcolor (RED, Pressed);
		ege::setfillcolor (WHITE, Click);
		ege::bar (0, 0, Width, Height, Hover);
		ege::bar (0, 0, Width, Height, Released);
		ege::bar (0, 0, Width, Height, Pressed);
		ege::bar (0, 0, Width, Height, Click);
		// ege::fillellipse (50, 50, 50, 50, Click);
		// Click 是点击范围，只要不是纯黑色都是有效区域，
		// 这一圈只不过是用内置函数了，实际上可以用任何图片

		this -> X = X;  this -> Width  = Width;
		this -> Y = Y;  this -> Height = Height;
		this -> UID = UID;
	}

	BUTTON (int X, int Y, ege::PIMAGE Hover, ege::PIMAGE Released, ege::PIMAGE Pressed, ege::PIMAGE Click = nullptr,
	        int UID = GetUID ()):HOVER (1),RELEASED (2), PRESSED (3) {
		// 用图像初始化
		int HW = ege::getwidth (Hover),    HH = ege::getheight (Hover),
			RW = ege::getwidth (Released), RH = ege::getheight (Released),
			PW = ege::getwidth (Pressed),  PH = ege::getheight (Pressed);
		this -> Hover = ege::newimage (HW, HH);
		this -> Released = ege::newimage (RW, RH);
		this -> Pressed = ege::newimage (PW, PH);
		if (Click == nullptr) {
			int MW = Max (HW, Max (RW, PW));
			int MH = Max (HH, Max (RH, PH));
			this -> Click = ege::newimage (MW, MH);
			ege::setfillcolor (WHITE, this -> Click);
			ege::bar (0, 0, MW, MH, this -> Click);
		} else {
			this -> Click = ege::newimage (ege::getwidth (Click), ege::getheight (Click));
			CopyImage (this -> Click, Click);
		}
		// 有效面积最大化
		CopyImage (this ->    Hover, Hover);
		CopyImage (this -> Released, Released);
		CopyImage (this ->  Pressed, Pressed);
		// ege::bar (0, 0, Width, Height, Pressed);

		this -> X = X;  this -> Y = Y;

	}
	void test (void) {
		ege::putimage (0, 0, Click);
	}
	void Show (int nx = -1, int ny = -1) {
		if (nx == -1 || ny == -1) {
			nx = this -> X;
			ny = this -> Y;
		}
		switch (CheckMouseEvent (nx, ny)) {
			case 1: ege::putimage (nx, ny, Hover); break;
			case 2: ege::putimage (nx, ny, Released); break;
			case 3: ege::putimage (nx, ny, Pressed); break;
			default : /*error*/ break;
		}
	}
	~BUTTON () {
		ege::delimage (Released);
		ege::delimage (Hover);
		ege::delimage (Pressed);
		ege::delimage (Click);
	}
};

int main () {

	ege::setinitmode (INIT_RENDERMANUAL);
	ege::initgraph (800, 800);
	ege::PIMAGE R = ege::newimage (), P = ege::newimage (), H = ege::newimage (), C = ege::newimage ();
	ege::getimage (R, "RELEASED.jpg");
	ege::getimage (P, "PRESSED.jpg");
	ege::getimage (H, "HOVER.jpg");
	ege::getimage (C, "CLICK.png");
	BUTTON test (100, 100, H, R, P, C);
	for (;ege::is_run (); ege::delay_fps (60), ege::cleardevice ()) {
		while (mousemsg ()) {
			GMouseMsg = getmouse ();
		}
		test.Show ();
	}
	ege::getch ();

	return 0;
}